/**
 * 
 */
package de.wesemann.mediahandbook.le.adapter;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import de.wesemann.jdedun.entities.mediahandbook.branches.MediahandbookBranche;
import de.wesemann.mediahandbook.le.R;

/**
 * @author Lars W.
 * 
 */
public class BranchAdapter extends ArrayAdapter<MediahandbookBranche> {

	private List<MediahandbookBranche> items;
	private final Activity context;

	public BranchAdapter(Activity context, List<MediahandbookBranche> data) {
		super(context, R.layout.branch_row, data);
		this.context = context;
		this.items = data;
	}

	static class ViewHolder {
		public TextView branchName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			LayoutInflater vi = context.getLayoutInflater();
			convertView = vi.inflate(R.layout.branch_row, null, true);

			holder = new ViewHolder();
			holder.branchName = (TextView) convertView.findViewById(R.id.tv_for_branchoverview_branch_name);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		MediahandbookBranche branch = items.get(position);
		if (branch.getName() != null) {
			holder.branchName.setText(branch.getName());
		}
		return convertView;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public MediahandbookBranche getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return items.get(position).hashCode();
	}

	public void setItems(List<MediahandbookBranche> branches) {
		notifyDataSetInvalidated();
		this.items = branches;
		notifyDataSetChanged();
	}

	public void addItems(List<MediahandbookBranche> branches) {
		if (branches != null && !(branches.isEmpty())) {
			// this.items.addAll(companies);
			for (MediahandbookBranche comp : branches) {
				add(comp);
			}
			notifyDataSetChanged();
		}

	}

}
